

import { createSlice,createAsyncThunk } from "@reduxjs/toolkit";

export const setTimeoutAction =createAsyncThunk('counter/getTimeout',()=>{
  const p1=new Promise((resolve)=>{
    
    setInterval(()=>{
        resolve({count:3})
    },200)
  })
  return p1.PromiseResult.count
})

const countSlice=createSlice({
 
name:"counter",

initialState:{
  count :0
},

reducers:{

  
increment(prev,action){
  
  prev.count+=action.payload
},
decrement(a,b){
a.count-=b.payload
}
},
extraReducers:{
  [setTimeoutAction.fulfilled](prev,action){
    prev.count+=action.payload
  },
  [setTimeoutAction.rejected](prev,action){
    console.log("很遗憾失败了")
  },
  [setTimeoutAction.pending](prev,action){
    console.log("正在进行中")
  }
}

})



// 通过createSlice创建的对象上有一个reducer属性,直接得到的就是当前的reducer函数,可以直接暴露出去给store使用
export default  countSlice.reducer

export const {
  increment,
  decrement
}=countSlice.actions